const api = require('api.js')
const func = require('./func.js')
const user_id = wx.getStorageSync('user_id')
const domainName = getApp().globalData.domainName
const license_list = getApp().globalData.license_list
const pilot_rank_list = getApp().globalData.pilot_rank_list
const uav_type_list = getApp().globalData.uav_type_list
const uav_level_list = getApp().globalData.uav_level_list
const nation_list = getApp().globalData.nation_list
const menuButtonObject = wx.getMenuButtonBoundingClientRect()
const windowWidth = wx.getSystemInfoSync().windowWidth

// pages/saler_invitation/saler_invitation.js
Page({
   /**
   * 页面的初始数据
   */
  data: {
    menuButtonObject,
    windowWidth,
    nation_list,
    license_list,
    uav_type_list,
    uav_level_list,
    pilot_rank_list,
    page_data:{},
    stu_info:{},
  },

  // 选择地址
  set_addr(e){
    let addr = e.detail.value
    this.setData({
      'page_data.province':addr[0],
      'page_data.city':addr[1],
      'page_data.area':addr[2],
      'page_data.postcode':e.detail.postcode
    })
  },

  // 选择执照种类
  select_license(e){
    this.setData({
      'page_data.license_id':this.data.license_list[e.detail.value].id,
    })
  },

  // 选择类别等级
  select_class(e){
    this.setData({
      'page_data.type_id' : this.data.uav_type_list[e.detail.value].id
    })
  },

  // 选择级别等级
  select_level(e){
    this.setData({
      'page_data.level_id': this.data.uav_level_list[value = e.detail.value].id
    })
  },

  // 选择操控员等级
  stu_set_rank(e){
    this.setData({
      'page_data.rank_id' : this.data.pilot_rank_list[e.detail.value].id
    })
  },


  // 前往实名认证页面
  go_cert_page(){
    console.log('前往实名认证页面')
    getApp().get_vibration()
    this.go_cert = 1  // 标记页面前往认证，以备页面返回时，重新加载该用户的信息
    wx.navigateTo({
      url: '/pages/users/identification/identification?user_id=' + this.user_id,
    })
  },

  bindgetphonenumber(e){
    let phone_code = e.detail.code
    if(phone_code){
      api.getphonenumber(this,this.user_id,phone_code)
    }else{
      wx.showModal({
        content: '微信授权失败，请稍后重试！',
        showCancel: false,
        title: '提示',
      })
    }
  },

  go_index(){
    getApp().get_vibration()
    wx.switchTab({url: '/pages/index/index'})
  },

  // 选择图片
  pickup_photo(){
    wx.chooseMedia({
      mediaType:['image'],
      count: 1,
      sizeType:['compressed'],
      success: res => {
        let tempFile = res.tempFiles[0]
        if(tempFile.size>500000){
          wx.showModal({
            content: '上传照片不可超过500KB',
            showCancel: false,
            title: '提示',
          })
          return
        }
        // 如果有jg_stu_id，那么就直接回传服务器
        if(this.data.stu_info.jg_stu_id){
          wx.showLoading()
          wx.uploadFile({
            filePath: tempFile.tempFilePath,
            name: 'files',
            url: domainName + '/register_index_update_stuPhoto',
            formData:{jg_stu_id:this.data.stu_info.jg_stu_id},
            header : {'content-type':'application/x-www-form-urlencoded'},
            success: res => {
              wx.hideLoading()
              // console.log('res',res.data)
              if(JSON.parse(res.data).update_res){
                // console.log('ok')
                wx.showToast({title: '更新完成！'})
                this.setData({
                  tempFilePath:tempFile.tempFilePath
                })
              }
            }
          })
        }else{
          this.setData({
            tempFilePath:tempFile.tempFilePath
          })
        }
      },
    })
  },

  // 提交
  submit_btn(){
    let submit_data = func.package_data(this)// 打包数据
    // console.log('submit_data',submit_data)
    let check_res = func.check_submitData(submit_data)  // 提交前数据检查
    if(!check_res){return}
    wx.showModal({
      content: '确定提交吗？',
      showCancel: true,
      title: '提示',
      success: res => {
        if(res.confirm){
            // 若近上传文字，那肯定是已经创建了【Jg_stu】的，且状态为【1，2】（申请中、被拒绝），其他状态【6，8，11】（已毕业、被退学、被删除）都必须重新上传头像
            if(this.jg_stu_id){
              api.upload_text(this,submit_data)
            }else{
              // 检查是否有照片临时文件【tempFilePath】，有则【upload_text_photo】，否则【upload_text】
              if(this.data.tempFilePath){
                api.upload_text_photo(this,submit_data)
              }else{
                wx.showModal({
                  content: '请上传您的头像',
                  showCancel: false,
                  title: '提示',
                })
              }
            }
        }
      },
    })
  },

  // 选择民族
  set_nation(e){
    this.setData({
      'page_data.nation':this.data.nation_list[e.detail.value].name
    })
  },

  // 输入地址
  input_addr(e){
    this.setData({
      'page_data.address': e.detail.value
    })
  },

  // 输入邮箱
  input_email(e){
    this.setData({
      'page_data.email':e.detail.value,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 首先判断该链接是否已过期
    console.log('options',options)
    func.check_pageInvalid(options.invalid_time)
    let page_data = JSON.parse(decodeURIComponent(options.page_data || {}))
    this.setData({page_data:page_data})
    this.jg_id = page_data.jg_id
    this.user_id = user_id
    if(user_id){
      api.page_init(this, user_id, this.jg_id)
    }else{
      api.userLogin_pageInit(this, this.jg_id)
    }
  },

  onReady(e){
    let windowHeight = wx.getSystemInfoSync().windowHeight
    this.setData({
      windowHeight:windowHeight,
      belt_count:parseInt(windowHeight/100),
    })
  },

  onShow(){
    if(this.go_cert==1){
      api.query_basicInfo(this,this.user_id)
      this.go_cert==null // 如果前往 实名认证页面返回后，将 go_cert 改为null
    }
  },

})